﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace filestream
{
    class MyFileOption
    {
        public bool WriteTextFile(string filepath, string filecontent, Int16 WriteMethord)
        {
            bool i = true;
            try
            {
                if (WriteMethord == 0)
                {
                    FileStream textfile = File.Open(filepath, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(textfile, Encoding.Default);
                    sw.Write(filecontent);
                    i = true;
                    sw.Close();
                    textfile.Close();
                }
                else if (WriteMethord == 1)
                {
                    FileStream textfile = File.Open(filepath, FileMode.Append, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(textfile, Encoding.Default);
                    sw.Write(filecontent);
                    i = true;
                    sw.Close();
                    textfile.Close();
                }
                return i;
            }
            catch
            {
                i = false;
                return i;
            }
        }

        internal bool WriteTexFile(string filepath, string filecontent, short v)
        {
            throw new NotImplementedException();
        }

        public bool DiskFileOption(string SourcePath, string TargetPath, Int16 OptionMethord)
        {
            bool i = true;
            try
            {
                if (OptionMethord == 0)
                {
                    FileStream textfile = File.Create(SourcePath);
                    textfile.Close();
                }
                else if (OptionMethord == 1)
                {
                    File.Delete(SourcePath);
                }
                else if (OptionMethord == 2)
                {
                    File.Copy(SourcePath, TargetPath, true);
                }
                else if (OptionMethord == 3)
                {
                    File.Move(SourcePath, TargetPath);
                }
                return i;
            }
            catch
            {
                i = false;
                return i;
            }
        }
        public void ReadTextFile(string filepath, out Int16 i, out string filecontent)
        {
            if (File.Exists(filepath))
            {
                try
                {
                    StreamReader textreader = new StreamReader(filepath, System.Text.Encoding.Default);
                    filecontent = textreader.ReadToEnd();
                    textreader.Close();
                    i = 1;
                }
                catch
                {
                    i = 2;
                    filecontent = "文件读取错误！";
                }
            }
            else
            {
                i = 0;
                filecontent = "文件或路径无效！";
            }
        }
        public bool DirectoryOption(string Directorypath, string TargetDirectorypath, Int16 OptionMethord, out string[] filesname)
        {
            bool k = true;
            filesname = null;
            if (Directory.Exists(Directorypath))
            {
                try
                {
                    if (OptionMethord == 0)
                    {
                        Directory.CreateDirectory(Directorypath);
                    }
                    else if (OptionMethord == 1)
                    {
                        Directory.Delete(Directorypath, true);
                    }
                    else if (OptionMethord == 2)
                    {
                        Directory.Move(Directorypath, TargetDirectorypath);
                    }
                    else if (OptionMethord == 3)
                    {
                        filesname = Directory.GetFiles(Directorypath);
                    }
                }
                catch
                {
                    k = false;
                }
            }
            else
            {
                Directory.CreateDirectory(Directorypath);
                k = true;
            }
            return k;
        }
    }
}